// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas.isoregion.regions;

import java.util.ArrayList;
import zombie.core.Color;
import zombie.core.Core;
import zombie.iso.areas.isoregion.IsoRegions;

public final class IsoChunkRegion implements IChunkRegion {
    private final IsoRegionManager manager;
    private boolean isInPool = false;
    private Color color;
    private int ID;
    private byte zLayer;
    private byte squareSize = 0;
    private byte roofCnt = 0;
    private byte chunkBorderSquaresCnt = 0;
    private final boolean[] enclosed = new boolean[4];
    private boolean enclosedCache = true;
    private final ArrayList<IsoChunkRegion> connectedNeighbors = new ArrayList();
    private final ArrayList<IsoChunkRegion> allNeighbors = new ArrayList();
    private boolean isDirtyEnclosed = false;
    private IsoWorldRegion isoWorldRegion;

    public int getID() {
        return this.ID;
    }

    public int getSquareSize() {
        return this.squareSize;
    }

    public Color getColor() {
        return this.color;
    }

    public int getzLayer() {
        return this.zLayer;
    }

    public IsoWorldRegion getIsoWorldRegion() {
        return this.isoWorldRegion;
    }

    public void setIsoWorldRegion(IsoWorldRegion mr) {
        this.isoWorldRegion = mr;
    }

    protected boolean isInPool() {
        return this.isInPool;
    }

    protected IsoChunkRegion(IsoRegionManager isoRegionManager) {
        this.manager = isoRegionManager;
    }

    protected void init(int int0, int int1) {
        this.isInPool = false;
        this.ID = int0;
        this.zLayer = (byte)int1;
        this.resetChunkBorderSquaresCnt();
        if (this.color == null) {
            this.color = this.manager.getColor();
        }

        this.squareSize = 0;
        this.roofCnt = 0;
        this.resetEnclosed();
    }

    protected IsoChunkRegion reset() {
        this.isInPool = true;
        this.unlinkNeighbors();
        IsoWorldRegion isoWorldRegionx = this.unlinkFromIsoWorldRegion();
        if (isoWorldRegionx != null && isoWorldRegionx.size() <= 0) {
            if (Core.bDebug) {
                throw new RuntimeException("ChunkRegion.reset IsoChunkRegion has IsoWorldRegion with 0 members.");
            }

            this.manager.releaseIsoWorldRegion(isoWorldRegionx);
            IsoRegions.warn("ChunkRegion.reset IsoChunkRegion has IsoWorldRegion with 0 members.");
        }

        this.resetChunkBorderSquaresCnt();
        this.ID = -1;
        this.squareSize = 0;
        this.roofCnt = 0;
        this.resetEnclosed();
        return this;
    }

    public IsoWorldRegion unlinkFromIsoWorldRegion() {
        if (this.isoWorldRegion != null) {
            IsoWorldRegion isoWorldRegionx = this.isoWorldRegion;
            this.isoWorldRegion.removeIsoChunkRegion(this);
            this.isoWorldRegion = null;
            return isoWorldRegionx;
        } else {
            return null;
        }
    }

    public int getRoofCnt() {
        return this.roofCnt;
    }

    public void addRoof() {
        this.roofCnt++;
        if (this.roofCnt > this.squareSize) {
            IsoRegions.warn("ChunkRegion.addRoof roofCount exceed squareSize.");
            this.roofCnt = this.squareSize;
        } else {
            if (this.isoWorldRegion != null) {
                this.isoWorldRegion.addRoof();
            }
        }
    }

    public void resetRoofCnt() {
        if (this.isoWorldRegion != null) {
            this.isoWorldRegion.removeRoofs(this.roofCnt);
        }

        this.roofCnt = 0;
    }

    public void addSquareCount() {
        this.squareSize++;
    }

    public int getChunkBorderSquaresCnt() {
        return this.chunkBorderSquaresCnt;
    }

    public void addChunkBorderSquaresCnt() {
        this.chunkBorderSquaresCnt++;
    }

    protected void removeChunkBorderSquaresCnt() {
        this.chunkBorderSquaresCnt--;
        if (this.chunkBorderSquaresCnt < 0) {
            this.chunkBorderSquaresCnt = 0;
        }
    }

    protected void resetChunkBorderSquaresCnt() {
        this.chunkBorderSquaresCnt = 0;
    }

    private void resetEnclosed() {
        for (byte _byte = 0; _byte < 4; _byte++) {
            this.enclosed[_byte] = true;
        }

        this.isDirtyEnclosed = false;
        this.enclosedCache = true;
    }

    public void setEnclosed(byte dir, boolean b) {
        this.isDirtyEnclosed = true;
        this.enclosed[dir] = b;
    }

    protected void setDirtyEnclosed() {
        this.isDirtyEnclosed = true;
        if (this.isoWorldRegion != null) {
            this.isoWorldRegion.setDirtyEnclosed();
        }
    }

    public boolean getIsEnclosed() {
        if (!this.isDirtyEnclosed) {
            return this.enclosedCache;
        } else {
            this.isDirtyEnclosed = false;
            this.enclosedCache = true;

            for (byte _byte = 0; _byte < 4; _byte++) {
                if (!this.enclosed[_byte]) {
                    this.enclosedCache = false;
                }
            }

            if (this.isoWorldRegion != null) {
                this.isoWorldRegion.setDirtyEnclosed();
            }

            return this.enclosedCache;
        }
    }

    public ArrayList<IsoChunkRegion> getConnectedNeighbors() {
        return this.connectedNeighbors;
    }

    public void addConnectedNeighbor(IsoChunkRegion neighbor) {
        if (neighbor != null) {
            if (!this.connectedNeighbors.contains(neighbor)) {
                this.connectedNeighbors.add(neighbor);
            }
        }
    }

    protected void removeConnectedNeighbor(IsoChunkRegion isoChunkRegion0) {
        this.connectedNeighbors.remove(isoChunkRegion0);
    }

    public int getNeighborCount() {
        return this.allNeighbors.size();
    }

    protected ArrayList<IsoChunkRegion> getAllNeighbors() {
        return this.allNeighbors;
    }

    public void addNeighbor(IsoChunkRegion neighbor) {
        if (neighbor != null) {
            if (!this.allNeighbors.contains(neighbor)) {
                this.allNeighbors.add(neighbor);
            }
        }
    }

    protected void removeNeighbor(IsoChunkRegion isoChunkRegion0) {
        this.allNeighbors.remove(isoChunkRegion0);
    }

    protected void unlinkNeighbors() {
        for (int int0 = 0; int0 < this.connectedNeighbors.size(); int0++) {
            IsoChunkRegion isoChunkRegion1 = (IsoChunkRegion)this.connectedNeighbors.get(int0);
            isoChunkRegion1.removeConnectedNeighbor(this);
        }

        this.connectedNeighbors.clear();

        for (int int1 = 0; int1 < this.allNeighbors.size(); int1++) {
            IsoChunkRegion isoChunkRegion2 = (IsoChunkRegion)this.allNeighbors.get(int1);
            isoChunkRegion2.removeNeighbor(this);
        }

        this.allNeighbors.clear();
    }

    public ArrayList<IsoChunkRegion> getDebugConnectedNeighborCopy() {
        ArrayList arrayList = new ArrayList();
        if (this.connectedNeighbors.size() == 0) {
            return arrayList;
        } else {
            arrayList.addAll(this.connectedNeighbors);
            return arrayList;
        }
    }

    public boolean containsConnectedNeighbor(IsoChunkRegion n) {
        return this.connectedNeighbors.contains(n);
    }

    public boolean containsConnectedNeighborID(int id) {
        if (this.connectedNeighbors.size() == 0) {
            return false;
        } else {
            for (int _int = 0; _int < this.connectedNeighbors.size(); _int++) {
                IsoChunkRegion isoChunkRegion = (IsoChunkRegion)this.connectedNeighbors.get(_int);
                if (isoChunkRegion.getID() == id) {
                    return true;
                }
            }

            return false;
        }
    }

    public IsoChunkRegion getConnectedNeighborWithLargestIsoWorldRegion() {
        if (this.connectedNeighbors.size() == 0) {
            return null;
        } else {
            IsoWorldRegion isoWorldRegion0 = null;
            IsoChunkRegion isoChunkRegion1 = null;

            for (int _int = 0; _int < this.connectedNeighbors.size(); _int++) {
                IsoChunkRegion isoChunkRegion2 = (IsoChunkRegion)this.connectedNeighbors.get(_int);
                IsoWorldRegion isoWorldRegion1 = isoChunkRegion2.getIsoWorldRegion();
                if (isoWorldRegion1 != null && (isoWorldRegion0 == null || isoWorldRegion1.getSquareSize() > isoWorldRegion0.getSquareSize())) {
                    isoWorldRegion0 = isoWorldRegion1;
                    isoChunkRegion1 = isoChunkRegion2;
                }
            }

            return isoChunkRegion1;
        }
    }

    protected IsoChunkRegion getFirstNeighborWithIsoWorldRegion() {
        if (this.connectedNeighbors.size() == 0) {
            return null;
        } else {
            for (int _int = 0; _int < this.connectedNeighbors.size(); _int++) {
                IsoChunkRegion isoChunkRegion1 = (IsoChunkRegion)this.connectedNeighbors.get(_int);
                IsoWorldRegion isoWorldRegionx = isoChunkRegion1.getIsoWorldRegion();
                if (isoWorldRegionx != null) {
                    return isoChunkRegion1;
                }
            }

            return null;
        }
    }
}
